#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Set_up.py
@Time    :   2022/04/13 18:00:00
@Author  :   WangWei 
@Version :   1.0
@Contact :   wangwei@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''
# here put the import lib

import datetime
import  os
from ssl import CHANNEL_BINDING_TYPES
from xml.dom.expatbuilder import FilterVisibilityController


# HW config
#CAN
drive_type = 'USE_TS_TC1014_DEVICE'   # USE_TS_TC1014_DEVICE /   USE_TS_VIRTUAL_DEVICE / USE_TS_TC1011_DEVICE

CAN_Type = 'CANFD'
if CAN_Type == 'CAN':
    configure_baudrate = {'CAN_Type':'CAN','ArbRate':500,'DataRate':500,'TermResistor':True}
else:
    configure_baudrate = {'CAN_Type':'CANFD','ArbRate':500,'DataRate':2000,'TermResistor':True}

# CAN_Type2 = 'CANFD'
# if CAN_Type2 == 'CAN':
#     configure_baudrate2 = {'CAN_Type':'CAN','ArbRate':500,'DataRate':500,'TermResistor':True}
# else:
#     configure_baudrate2 = {'CAN_Type':'CANFD','ArbRate':500,'DataRate':2000,'TermResistor':True}


#Dbc define
root = os.getcwd()
project_DBC_File_CAN = os.path.join(root, r'data\MRR_Matrix_V1.4_20241020.dbc')
blfname = datetime.datetime.now().strftime('%y%m%d') + '.blf'
project_BLF_File_CAN = os.path.join(root, f"blf\{blfname}")
# project_DBC_File_CAN = os.path.join(root, r'data\FX12_Matrix_CRRL_V1.6_20220531.dbc')
NetName = 'MRR'
nodeName= 'ADC'
# EnableNode = ['CGW_Safety','EPS_Safety','APA_Safety','ABS_ESP_Safety','FVC_Safety']
EnableNode = ['MRR','ADC']

#Power Config
source='Power'           #电源名称，可以使用NIMAX查看
baudrate=9600

#Dbc UDS
reqID = 0x7F2
resID = 0x7FA
AFctID = 0x7DF

modbusCOM = 'COM8'

# 0x22 DID
# 底层软件Bootload版本号 F180
BootSwId= [0x56,0x31,0x2E,0x32,0x30,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20] # 0xF180
# 备用零件号
CowinSparePartNumber = [0x46,0x30,0x32,0x2D,0x33,0x36,0x30,0x32,0x30,0x31,0x34,0x2D,0x30,0x31,0x41,0x41,0x20,0x20,0x20,0x20]  # 0xF187
# 软件版本号
ManufacturerECUSoftwareVersionNumber = [0x30,0x30,0x2E,0x30,0x30,0x2E,0x30,0x33] # 0xF188
# 生成日期
ProgrammingDate = [0x20,0x22,0x07,0x25]  # 0xF199
# 雷达角度
Radarangle = 0x2D # 0x3F1A

softname = 'MRR_EM2EH_GaoJie_App_1.00.00_Boot_1.0_CT_6.4.4.1_6.4.4.1_Pro.XCDII.zip'

zipfile_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +softname
error_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus.zip'
oldzipfile_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'old.zip'
errorApp_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus\\001_Failed_App.hex'
errorBoot_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus\\002_Failed_Boot.hex'

CTBoot=[6,4,4,1]

'''BYD产品DID'''
# 硬件版本号 F193 
ECUHardwareVersionNumber = '0b 17 09 0d 00'# 美信【01 16 09 06 00】      70331【0b 17 09 0d 00】  4323【05 16 0c 1f 00 】 9840【06 16 0c 1f 00 】
# 软件编码 F194
ECUSoftwareCode = '003100143E70000000'
# DATASET软件序列号 FF94
DATASETSoftwareNumber = []
# DATASET软件版本号 FF95
DATASETSoftwareVersionNumber = []
# 底层软件Bootload版本号 F180
bootyear = [24]
bootmonth = [10]
bootday = [25]
bootchangeNum = [0]
bootver = [10]
bootweek = [43]
# bootweek.append(datetime.date(2000 + bootyear[0], bootmonth[0], bootday[0]).isocalendar().week)
VehicleManufacturerECUBootSoftwareVersionNumber = [0x62,0xF1,0X80] + bootyear + bootweek + bootchangeNum + bootver + [0] + [0] + bootyear + bootmonth + bootday

# 软件版本号 F195
APP_ver = '10000'
softver = []
hexver = hex(int(APP_ver)).replace('0x','').zfill(4)
softver.append(int(hexver[0:2],16))
softver.append(int(hexver[2:4],16))
softyear =[24]
softmonth = [10]
softday = [25]
ECUSoftwareVersionNumber = softver + softyear + softmonth + softday + [0]

# 生产模式 3401
FactoryMode = []
# 配置字
VariantCoding = [   ('14 3D 70 01 60 00 03 70'),              
                    ('14 3E 70 02 50 00 03 42'), 
                    # ('12 44 70 01 10 80 07 0B'), 
                    # ('12 06 70 01 10 80 07 75'), 
				]

# 初始存在故障
InitialFailure = [[0xAC,0xD1,0x78,0x09],[0xAC,0xD2,0x78,0x09],[0xAC,0xE6,0x55,0x09]]

Dut_Type = 'EM2EH_GaoJie'

DTC_list = {    'DTC_VBAT_HIGH':                                [0xAC,0xCE,0x17],		
                'DTC_VBAT_LOW':                                 [0xAC,0xCF,0x16],		
				# 'DTC_PCAN_BUSOFF':                              [0xC0,0x73,0x88],			
				'DTC_SFCAN_BUSOFF':                             [0xC1,0xD7,0x88],			
				'DTC_PRODUCTION_MODE_ACTIVE':                   [0xAC,0xD0,0x00],
                'DTC_ALIGNMENT_NEVER_DONE':                     [0xAC,0xD1,0x78],
                'DTC_ALIGNMENT_NOT_DONE':                       [0xAC,0xD2,0x78],
                'DTC_VARIANT_NOT_CODED':                        [0xAC,0xE6,0x55],
                'DTC_MRR_SYSTEM_BLIND':                         [0xAC,0xE8,0x76],
                'DTC_MRR_SENSOR_BLIND':                         [0xAC,0xE9,0x76],
                'DTC_MRR_VERTICAL_MISALIGNED':                  [0xAC,0xEA,0x94],
                'DTC_MRR_HORIZONTAL_MISALIGNED':                [0xAC,0xEB,0x94],
                'DTC_FACTORYDATA_FAILURE':                      [0xAC,0xD4,0x88],
                # 'DTC_HW_UC_PFLASH':                             [0xAC,0xD5,0x41],                       #new_241024
                'DTC_INTERNAL_POWER_MANAGEMENT_SYS_FAILURE':    [0xAC,0xD6,0x40],
                # 'DTC_INTERNAL_UC_SYS_FAILURE':                  [0xAC,0xD7,0x40],                       #new_241024
                'DTC_INTERNAL_HW_FAILURE':                      [0xAC,0xD8,0x04],
                'DTC_POWER_MANAGEMENT_CHIP_VOLTAGE_HIGH':       [0xAC,0xD9,0x17],
                # 'DTC_MMIC_VOLTAGE_HIGH':                        [0xAC,0xDA,0x17],                       #new_241024
                'DTC_POWER_MANAGEMENT_CHIP_VOLTAGE_LOW':        [0xAC,0xDB,0x16],
                # 'DTC_MMIC_VOLTAGE_LOW':                         [0xAC,0xDC,0x16],                       #new_241024
                'DTC_RADAR_MODULATION_CONFIGURE':               [0xAC,0xDD,0x00],
                'DTC_RADAR_MODULATION':                         [0xAC,0xDE,0x00],
                'DTC_SW_FAILURE':                               [0xAC,0xDF,0x52],
                'DTC_SW_RIF_FAILURE':                           [0xAC,0xE0,0x00],
                'DTC_SW_TEMP_FAILURE':                          [0xAC,0xE1,0x52],
                'DTC_UC_TEMPERATURE_OUTOFRANGE':                [0xAC,0xE2,0x4B],
                # 'DTC_MMIC_TEMPERATURE_OVER_MAXIMUM':            [0xAC,0xE3,0x4B],                       #new_241024
                # 'DTC_MMIC_TEMPERATURE_OVER_OPERATION':          [0xAC,0xE4,0x4B],                       #new_241024
                # 'DTC_ADC_SELF_TEST_FAILURE':                    [0xAC,0xE5,0x00],                       #new_241024
                # 'DTC_CMRR_DACORE_OOS':                          [0xAC,0xEC,0x00],                       #new_241024
                'DTC_AntennaDiagram_INVALID':                   [0xAC,0xED,0x00],
                # 'DTC_VMC_ERROR':                                [0xAC,0xF0,0x54],                       #new_241024
                'DTC_Left_BCM_TIMEOUT':                         [0xC1,0xA8,0x87],
                # 'DTC_Left_BCM_CRC':                             [0x5C,0x67,0x62],
                # 'DTC_Left_BCM_ALC':                             [0x5C,0x66,0x02],
                'DTC_Left_BCM_Signal_Invalid':                  [0x5C,0x65,0x86],

                'DTC_Rear_BCM_TIMEOUT':                         [0xD0,0x03,0x04],           #NEW
                'DTC_Rear_BCM_CRC':                             [0x5C,0x49,0x00],           #NEW
                'DTC_Rear_BCM_ALC':                             [0x5C,0x36,0x82],           #NEW
                'DTC_Rear_BCM_Signal_Invalid':                  [0xD1,0x7B,0x86],           #NEW
                # 'DTC_Right_BCM_TIMEOUT':                        [0xC1,0xE9,0x87],           #NEW
                # 'DTC_Right_BCM_ALC':                            [0xC1,0xE3,0x82],           #NEW

                'DTC_Media_TIMEOUT':                            [0xC2,0x45,0x00],

                'DTC_Media_CRC':                                [0xC2,0xED,0x83],            #NEW
                # 'DTC_CS_TIMEOUT':                               [0xD1,0x01,0x87],            #new_241024
                # 'DTC_CS_Signal_Invalid':                        [0x5C,0x5F,0x86],            #new_241024

                'DTC_VCU_TIMEOUT':                              [0xD0,0xC2,0x86],
                'DTC_VCU_CRC':                                  [0xD2,0x71,0x83],
                'DTC_VCU_ALC':                                  [0xD2,0x71,0x82],
                'DTC_IPB_TIMEOUT':                              [0xC1,0x97,0x80],
                'DTC_IPB_CRC':                                  [0x5C,0x64,0x62],
                'DTC_IPB_ALC':                                  [0x5C,0x63,0x02],
                'DTC_IPB_Signal_Invalid':                       [0x5C,0x62,0x86],
                'DTC_EPS_TIMEOUT':                              [0xC1,0x34,0x00],
                'DTC_EPS_CRC':                                  [0xC4,0x20,0x83],
                'DTC_EPS_ALC':                                  [0xC4,0x20,0x82],
                'DTC_EPS_Signal_Invalid':                       [0xD0,0x17,0x86],
                'DTC_Time_SYNC_FAIL':                           [0xAC,0xEE,0x00],            #NEW

                #'''空气悬架'''
                # 'DTC_DiSus_Signal_Invalid':                     [0xD1,0x3F,0x81],            #NEW
                # 'DTC_DiSus_ALC':                                [0xD0,0xF5,0x82],            #NEW
                # 'DTC_DiSus_CRC':                                [0xD1,0x3F,0x83],            #NEW
                # 'DTC_DiSus_TIMEOUT':                            [0xD1,0x2E,0x87],            #NEW
}

'''rollingcounter 是否一致 错误个数 数据长度'''
PCAN_list_BOCH = {  0x659: [0,True,0,64],
                    0x65A: [0,True,0,64],
                    0x65B: [0,True,0,64],
                    0x65C: [0,True,0,64],
                    0x65D: [0,True,0,64],
                    0x65E: [0,True,0,64],
                    0x65F: [0,True,0,64],
                    0x660: [0,True,0,64],
                    0x661: [0,True,0,64],
                    0x662: [0,True,0,64],
                    0x663: [0,True,0,64],
                    0x664: [0,True,0,64],
                    0x665: [0,True,0,64],
                    0x666: [0,True,0,64],
                    0x667: [0,True,0,64],
                    0x668: [0,True,0,64],
                    0x669: [0,True,0,64],
                    0x66A: [0,True,0,64],
                    0x66B: [0,True,0,64],
                    0x66C: [0,True,0,64],
                    0x66D: [0,True,0,64],
                    0x66E: [0,True,0,64],
                    0x66F: [0,True,0,8],
                    0x670: [0,True,0,64],
}

startCheck = False

MRR_0x66E_data = []
MRR_0x66E_time = []


MRR_0x081_data = []



# import csv                 
# # data file
# root = os.getcwd()
# DTC_data_file = os.path.join(root, r'data\DTCdata.csv')
# # DTC_data_table = []
# # with open(DTC_data_file, 'r') as f:
# #     for row in csv.reader(f):
# #         DTC_data_table.append(row)
# # DTC_data = DTC_data_table[1:]

# import pandas as pd
# import numpy as np
# data = pd.read_csv(DTC_data_file)
# print(data)
# act_vehicle_type = 'A02'
# DTC_data = np.array(data).tolist()
# print(DTC_data)
# if act_vehicle_type in DTC_data[1][9:]:
#     print("111111")

# allure generate ..\allure-results\ -o .\report --clean
# allure open .\report



sessionControl_default = [([0x10,0x01],[0x50,0x01,0x00,0x32,0x13,0x88]),    
                          ([0x10,0x03],[0x50,0x03,0x00,0x32,0x13,0x88])]

sessionControl_extend = [([0x10,0x03],[0x50,0x03,0x00,0x32,0x13,0x88]),    
                         ([0x10,0x01],[0x50,0x01,0x00,0x32,0x13,0x88])]

sessionControl_Suppress_default = [([0x10,0x81],[]),    
                                   ([0x10,0x83],[])]    

sessionControl_Suppress_extend = [([0x10,0x83],[]),    
                                 ([0x10,0x81],[])] 

sessionControl_NRC_phy = [([0x10],[0x7F,0x10,0x13]),        
                          ([0x10,0x01,0xFF],[0x7F,0x10,0x13]),
                          ([0x10,0x03,0x10],[0x7F,0x10,0x13]),
                          ([0x10,0x05,0x06],[0x7F,0x10,0x13]),
                          ([0x10,0x04],[0x7F,0x10,0x12]),   
                          ([0x10,0x05],[0x7F,0x10,0x12]), 
                          ([0x10,0x06],[0x7F,0x10,0x12]),
                          ([0x23],[0x7F,0x23,0x11]),
                          ([0x2A],[0x7F,0x2A,0x11]),
                          ([0x2C],[0x7F,0x2C,0x11]),
                          ([0x2F],[0x7F,0x2F,0x11]),
                          ]

sessionControl_SuppressPosMsg_NRC_phy = [     
                                         ([0x10,0x81,0xFF],[0x7F,0x10,0x13]),
                                         ([0x10,0x83,0x10],[0x7F,0x10,0x13]),
                                         ([0x10,0x84],[0x7F,0x10,0x12]),   
                                         ([0x10,0x85],[0x7F,0x10,0x12]), 
                                         ([0x10,0x86],[0x7F,0x10,0x12])]

sessionControl_NRC7E_phy = [([0x10,0x02],[0x7F,0x10,0x7E])]

sessionControl_NRC13_func = [([0x10],[0x7F,0x10,0x13]),        
                             ([0x10,0x01,0xFF],[0x7F,0x10,0x13]),
                             ([0x10,0x03,0x10],[0x7F,0x10,0x13])]

sessionControl_noResp_default_func = [([0x10,0x04],[]),   
                                      ([0x10,0x05],[]), 
                                      ([0x10,0x06],[]),
                                      ([0x10,0x02],[]),
                                      ([0x27,0x03],[]),
                                      ([0x2E,0x3F,0x01],[]),
                                      ([0x28,0x00,0x01],[]),
                                      ([0x85,0x01],[]),
                                      ([0x23],[]),
                                      ([0x2A],[]),
                                      ([0x2C],[])]


sessionControl_suppressPosMsgBit_NRC13 = [([0x10,0x81,0xFF],[0x7F,0x10,0x13]),
                                          ([0x10,0x83,0x10],[0x7F,0x10,0x13])]

sessionControl_noResp_suppressPosMsgBit_default_func = [([0x10,0x84],[]),   
                                                        ([0x10,0x85],[]), 
                                                        ([0x10,0x86],[]),
                                                        ([0x10,0x82],[])]

sessionControl_noResp_suppressPosMsgBit_extend_func = [([0x10,0x84],[]),   
                                                       ([0x10,0x85],[]), 
                                                       ([0x10,0x86],[])]

boot_sessionControl_NRC = [([0x10],[0x7F,0x10,0x13]),         
                           ([0x10,0x02,0xFF],[0x7F,0x10,0x13]),
                           ([0x10,0x02,0x10],[0x7F,0x10,0x13]),
                           ([0x10,0x04],[0x7F,0x10,0x12]),      
                           ([0x10,0x05],[0x7F,0x10,0x12]),
                           ([0x10,0x06],[0x7F,0x10,0x12])]

boot_sessionControl_suppressPosMsgBit_NRC = [([0x10,0x82,0xFF],[0x7F,0x10,0x13]), 
                                             ([0x10,0x82,0x10],[0x7F,0x10,0x13]),
                                             ([0x10,0x84],[0x7F,0x10,0x12]),     
                                             ([0x10,0x85],[0x7F,0x10,0x12]),
                                             ([0x10,0x86],[0x7F,0x10,0x12])]

ClearDtcInfo_NRC13_Func = [([0x14],[0x7F,0x14,0x13]),                  
                           ([0x14,0xFF],[0x7F,0x14,0x13]),
                           ([0x14,0xFF,0xFF],[0x7F,0x14,0x13]),
                           ([0x14,0xFF,0xFF,0xFF,0xFF],[0x7F,0x14,0x13]),
                           ([0x14,0x11,0x22,0xFF,0xFF],[0x7F,0x14,0x13])]
